// pages/d-center/dealerOrder.js
var app = getApp()
Page({
  data: {
    statusType: ["待发货", "已发货","已取消"],
    currentType: 0,
    tabClass: ["", "", "", "", ""],
    bodyHeight: null,
    index: 1,
    isFinish: false,
    orderList: [],
  },
  logisticsTap(e) {
    var orderId = e.currentTarget.dataset.id;
    if (orderId) {
      wx.navigateTo({
        url: '/pages/logistics/logistics?id=' + orderId,
      })
    } else {
      wx.showToast({
        title: '等待物流公司发货中',
        icon: 'none'
      })
      return
    }
  },
  statusTap: function (e) {
    console.log(e);
    console.log(e.hasOwnProperty("currentTarget"))
    if (e && e.hasOwnProperty("currentTarget")) {
      this.setData({
        currentType: e.currentTarget.dataset.index
      });
    } else {
      this.setData({
        currentType: e.currentType
      });
    }
    this.setData({
      orderList: [],
      isFinish: false,
      index: 1,
    })
    this.getData();
    // this.onShow();
  },
  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/dealerOrder-detail/dealerOrder-detail?id=" + orderId + '&share=1'
    })
  },
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
            mask: true
          });
          app.postForm("dealerorder/cancel_dealer_order", {
            CustId: app.globalData.uid,
            DealerOrderId: orderId
          }, res => {
            wx.hideLoading();
            that.statusTap({
              currentType: 1
            });
            if (res.code == -1) {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
            } else {
              wx.showToast({
                title: '取消订单成功'
              })
            }
          })
        }
      }
    })
  },
  toPayTap: function (e) {
    wx.showLoading();
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;

    let payData = {
      OrderIds: orderId,
      OpenId: app.globalData.openid,
      Type: 'JSAPI'
    }
    app.postForm('order/pay_order_by_orderids', payData, rep => {
      // console.log(rep)
      wx.hideLoading();
      that.wxPay(rep)
    })
    
  },
  wxPay(params) {
    console.log(params)
    wx.requestPayment({
      timeStamp: params.timeStamp,
      nonceStr: params.nonceStr,
      package: params.package,
      signType: 'MD5',
      paySign: params.paySign,
      success: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: '支付成功',
          icon: 'success'
        })
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: '已取消支付',
          icon: 'none'
        })
      },
      complete: function (res) {
        wx.hideLoading();
        console.log(res);
      }
    })
  },
  onLoad: function (e) {
    var that = this;
    var currentType;
    wx.hideShareMenu()

    if (app.globalData.iphone == true) {
      that.setData({
        iphone: 'iphone'
      })
    }

    that.statusTap({
      currentType: 0
    });
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  getOrderStatistics: function () {
    var that = this;
  },
  toConfirmTap: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.id;
    let formId = e.detail.formId;
    wx.showModal({
      title: '确认您已收到商品？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中...',
            mask: true
          });
          app.postForm("dealerorder/receive_dealer_order", {
            CustId: app.globalData.uid,
            DealerOrderId: orderId
          }, res => {
            wx.hideLoading({
              success: (res) => {},
            })
            if (res && res.code == '0') {
              that.statusTap({
                currentType: 2
              });
            }
          })
        }
      }
    })
  },

  getData: function (status, index) {
    let _this = this;
    if (this.data.isFinish) {
      // wx.showToast({
      //   title: '没有更多了~',
      // })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    let sta = 0;
    switch (this.data.currentType) {
      case 0:
        sta = 1
        break;
      case 1:
        sta = 2
        break;
      case 2:
        sta = 13
        break;
      case 3:
        sta = 13
        break;
      default:
        break;
    }
    app.get(`dealerorder/test_get_dealer_orders?DealerCustId=${app.globalData.uid}&Submitter=${app.globalData.uid}&Status=${sta}&page=${this.data.index}&size=20`, (res) => {
      wx.hideLoading();
      if (res && res.code == "0") {
        if (res.result.length > 0) {
          _this.setData({
            orderList: _this.data.orderList.concat(res.result),
            index: this.data.index + 1
          });
        } else {
          _this.setData({
            isFinish: true,
          })
          // wx.showToast({
          //   title: '没有更多了~',
          // })
        }
      }
    })
  },
  // getData() {
  //   app.get(`dealerorder/get_dealer_orders?DealerCustId=${ 4254 }`, res => {
  //     console.log(res)
  //     if( res.result.length > 0 ){
  //       this.setData({
  //         orderList: res.result.map(item=>{
  //           item.CreateTime=item.CreateTime.substr(0,19);       
  //           return item;
  //         })
  //       })
  //     }
  //   })
  // },
  onShow: function (e) {
    // 获取订单列表
    // wx.showLoading();
    var that = this;
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  }
})